package cn.zhizihua.controller;


import cn.zhizihua.domain.po.Business;
import cn.zhizihua.domain.po.User;
import cn.zhizihua.domain.query.BusinessQuery;
import cn.zhizihua.domain.query.UserQuery;
import cn.zhizihua.domain.vo.ResultBean;
import cn.zhizihua.exception.AppException;
import cn.zhizihua.exception.AppExceptionCodeMsg;
import cn.zhizihua.service.IUserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "用户管理接口")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    IUserService userService;

    //有id为修改用户，无为添加用户
    @ApiOperation("用户添加或修改")
    @PostMapping      //  请求方式 + 请求路径   post  /user
    public ResultBean save(@RequestBody User user){
        System.out.println("----UserController-----save--------");
        System.out.println(user);
        //添加或修改用户信息
        boolean saveOrUpdate = userService.saveOrUpdate(user);
        //判断是否添加成功
        if (!saveOrUpdate){
            throw new AppException(AppExceptionCodeMsg.SAVE_OR_UPDATE_FAILE);
        }
        //返回成功
        return ResultBean.SUCCESS();
    }


    //  delete   /user/1003
    //删除用户  （修改用户delTag，标记为删除）
    @ApiOperation("根据用户id删除用户信息")
    @DeleteMapping("/{id}")   // http://localhost:8080/user/1003
    public ResultBean del(@PathVariable("id") Integer id){
        System.out.println("-------UserController--------del-------------");
        System.out.println(id);
        /*//标记为删除
        return userService.changeDelTag(id);*/
        //采用mp逻辑删除
        boolean del = userService.removeById(id);
        if (!del){
            throw new AppException(AppExceptionCodeMsg.DELETE_FAIL);
        }
        return ResultBean.SUCCESS();
    }

    //  get   /user/1004
    @ApiOperation("根据用户id查询用户信息")
    @GetMapping("/{id}")   // http://localhost:8080/user/1003
    public ResultBean getById(@PathVariable("id") Integer id){
        System.out.println("-------UserController--------getById-------------");
        System.out.println(id);
        //id查询用户信息
        User user = userService.getById(id);
        return ResultBean.SUCCESS(user);
    }

    @ApiOperation("查询全部用户信息")
    @GetMapping("/all")    //  get  /user/all
    public ResultBean getAll(){
        System.out.println("-------UserController-----getAll--------");
        //获取全部数据
        List<User> userList = userService.list();
        return ResultBean.SUCCESS(userList);
    }

    //TODO
//    @ApiOperation("分页查询用户信息")
//    @PostMapping("/page")   // post  /user/page
//    public ResultBean getByPage(@RequestBody UserQuery userQuery){
//        System.out.println("-------UserController--------getByPage-------------");
//        System.out.println(userQuery);
//        //设置分页
//        Page<User> page = new Page<>(userQuery.getPageNow(), userQuery.getPageCount());
//        //分页条件查询   page第x页，一页x条数据  userQuery.getQueryWrapper()模糊搜素条件
//        userService.page(page, userQuery.getQueryWrapper());
//        if (page.getTotal() == 0){
//            throw new AppException(AppExceptionCodeMsg.GET_BY_PAGE_FAILE);
//        }
//        //返回搜素信息
//        return ResultBean.SUCCESS(page);
//    }

    @ApiOperation("分页查询用户信息")
    @PostMapping("/page")    //  post  /xxx/all
    public ResultBean getByPage(@RequestBody UserQuery query){
        System.out.println("-------UserController-----page--------");
        System.out.println(query);
        Page<User> page = new Page<>(query.getPageNow(), query.getPageCount());
        userService.page(page,query.getQueryWrapper());
        if (page.getTotal() == 0){
            throw new AppException(AppExceptionCodeMsg.GET_BY_PAGE_FAILE);
        }
        return ResultBean.SUCCESS(page);
    }


    @ApiOperation("批量删除用户信息")
    @PostMapping("/batch")   // post + /food/batch
    public ResultBean batchDel(@RequestBody List<Integer> ids){
        System.out.println("----------FoodController---------batchDel---------------");
        System.out.println(ids);
        //批量删除
        boolean batchDel = userService.removeBatchByIds(ids);
        //判断批量删除是否成功
        if (!batchDel){
            throw new AppException(AppExceptionCodeMsg.BATCH_DEL_FAILE);
        }
        return ResultBean.SUCCESS();
    }

}
